const { $wx, $fn } = getApp()
module.exports = Behavior({
    behaviors: ['wx://form-field-group'],
    properties: {
        data: { type: Array, value: [] },
        gap: { type: Number, value: 20 },
        mb: { type: Number, value: 0 },
        labelWidth: { type: Number, value: 120 },
        className:String,
        style:String,
        defaultValue: null,
        y: { type:Boolean, value:true },
        labelWidth: Number,
        fontSize:Number,
        labelFontSize: Number,
        labelStyle:String,
        labelTextStyle:String,
        popErr:{ type:Boolean, value: true },       // 开启弹窗错误提示
        msgErr:{ type:Boolean, value: true },       // 开启红色错误提示
        changeError:{ type:Boolean, value: false }  // 改变输入内容时显示提示
    },
    data: {
        form:{}
    },
    start:0,    // 表示提交第一次出错
    lifetimes:{
        created(){},
        attached(){
            const { data } = this.data
            this.coptyData = JSON.parse(JSON.stringify( data ))
        },
        ready(){}
    },
    methods: {
        change({ detail, currentTarget:{ dataset: { param } } }){
            const { data, changeError, form } = this.data
            const row = data[param]
            row.value = detail
            const { r, name, rules } = row
            form[name] = detail
            // 全部改变时验证 || 已经提交一次出错时验证 && 必填 || rules 存在 || 包含固定的内个字段
            if( (changeError || this.start === 1) && ( r || rules || $wx.validate.changeRules( rules, name ) ) ){
                $wx.validate.changeValidate(row, detail)
                this.setData({ data})
            }
            if(!row.error){
                // if($fn.hasArray( row.data )){
                //     const idStr = row.idStr || $config.idStr
                //     const nameStr = row.nameStr  || $config.nameStr
                //     row.row = row.data.filter( v => v[idStr] === value )
                // }
                this.triggerEvent('change', row)
            }
        },
        _removeError(){
            this.data.data.forEach( v => {
                delete v.error
                delete v.errorMsg
            })
        },
        _getTypeParam(param){
            const { data } = this.data
            for(let i = 0; i < data.length; i ++){
                const { t, name } = data[i]
                const value  = param[name]
                if($fn.isEmpty(value)) continue
                switch(t){
                    case 'array':
                        param[name] = value.split(',')
                        break
                    case 'number':
                        param[name] = Number( value)
                        break
                    case 'bool':
                        param[name] = Boolean( value * 1 )
                        break
                    defalut:
                        break
                }
            }
            return param
        },
        async submit(param){
            const { data, popErr } = this.data || {}
            return new Promise((resolve)=>{
                if($wx.validate.submitValidate(data, param, popErr)){
                    param = this._getTypeParam(param)
                    resolve(param)
                }else{
                    this.start = 1
                }
                this.setData({ data })
            })
        },
        update(){
            const { data } = this.data
            console.log(123)
            this.setData({ data })
        },
        getValue( name ){
            const { data } = this.data
            if(name){
                const arr = data.filter(v => v.name === name)
                if(arr.length === 1){
                    return { [name]: arr[0].value }
                }
            }else{
                return data.map( v => ({ [v.name]: v.value }) )
            }
        },
        setValue(value){
            const { data } = this.data
            if($fn.hasObject(value)){
                for(let n in value){
                    const arr = data.filter( p => n === p.name)
                    if(arr.length === 1){
                        arr[0].value = value[n]
                    }
                }
            }else{
                data.forEach((v,i)=>v.value = '')
            }
            this.setData({ data })
        },
        reset(){
            const { defaultValue } = this.data
            let d = {}
            if($fn.hasObject(defaultValue)){
                d = defaultValue
            }else{
                this.coptyData.forEach( v => {
                    if($fn.isNotEmpty( v.value )){
                        d[v.name] = v.value
                    }
                })
            }
            this._removeError()
            this.setValue(d)
        },
        clear(){
            this._removeError()
            this.setValue()
         },
        getCopy(){ return this.coptyData }
    },
    observers: {
        'defaultValue': function( value ){
            if($fn.hasObject(value)){
                if(this.index === 1){
                    setTimeout(()=>this.setValue( value ))
                }else{
                    this.setValue( value )
                }
            }
            this.index  = 1
        }
    }
})